package kin.json;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.List;

import kin.system.util.StringUtil;
import kin.test.Us;

import org.apache.commons.beanutils.BeanUtils;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;

public class CustomSerializer extends JsonSerializer<Object> {

	
	
	@Override
	public void serialize(Object value, JsonGenerator jgen,
			SerializerProvider provider) throws IOException,
			JsonProcessingException {
		/*if (value == 1) {
            generator.writeString("one");
        } else if (value == 2) {
            generator.writeString("two");
        } else if (value == 3) {
            generator.writeString("three");
        } else {
            generator.writeString("A big number");
        }*/
	
		System.out.println("pp:"+value.getClass().getSimpleName());
		String packageName=value.getClass().getPackage().getName();
		Class type=value.getClass();
		System.out.println( "pp:"+type);
		
		if("kin.test".equals(packageName)){
			//System.out.println("oooo");
			String prop=StringUtil.fristLetterLowerCase(type.getSimpleName())+"_id";
			try {
				//jgen.writeFieldName(prop);
				//jgen.writeNumberField(prop,Integer.parseInt(BeanUtils.getProperty(value, prop)));
				
				//jgen.writeObjectField(prop, BeanUtils.getProperty(value, prop));
				
				jgen.writeObject(BeanUtils.getProperty(value, prop));
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		
		}
		if(type.isEnum()){
			
		}
		
		
		if(value instanceof Collection){
		System.out.println("tttt:"+type.getInterfaces()[0]);
		//jgen.writeStartArray();
		//jgen.writeEndArray();
		Collection c=(Collection) value;
		if(c==null&&c.size()==0){
			jgen.writeNull();
			
		}else{
			provider.defaultSerializeValue(value, jgen);
		}
		}
		
		
	}


   

	
}
